{% extends 'base.html' %}
{% load static %}

{% block title %}通知管理 - 维修管理系统{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <h1 class="mb-4">通知管理</h1>
            
            <!-- 搜索和筛选 -->
            <div class="card mb-4">
                <div class="card-body">
                    <form method="get" class="row g-3 align-items-center">
                        <div class="col-md-6">
                            <input type="text" name="search" class="form-control" 
                                   placeholder="搜索通知标题或内容..." value="{{ search_query }}">
                        </div>
                        <div class="col-md-3">
                            <select name="status" class="form-control">
                                <option value="">所有状态</option>
                                <option value="active" {% if status_filter == 'active' %}selected{% endif %}>激活</option>
                                <option value="inactive" {% if status_filter == 'inactive' %}selected{% endif %}>停用</option>
                                <option value="current" {% if status_filter == 'current' %}selected{% endif %}>当前有效</option>
                                <option value="expired" {% if status_filter == 'expired' %}selected{% endif %}>已过期</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <div class="d-grid gap-2 d-md-flex">
                                <button type="submit" class="btn btn-primary me-md-2">
                                    <i class="bi bi-search"></i> 搜索
                                </button>
                                <a href="{% url 'notifications:notification_list' %}" class="btn btn-outline-secondary">
                                    <i class="bi bi-arrow-clockwise"></i> 重置
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <!-- 操作按钮 -->
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h5 class="mb-0">通知列表</h5>
                <a href="{% url 'notifications:notification_create' %}" class="btn btn-primary">
                    <i class="bi bi-plus-circle"></i> 创建通知
                </a>
            </div>

            <!-- 通知列表 -->
            {% if notifications %}
            <div class="card">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>标题</th>
                                    <th>目标角色</th>
                                    <th>优先级</th>
                                    <th>状态</th>
                                    <th>有效期</th>
                                    <th>创建者</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for notification in notifications %}
                                <tr>
                                    <td>
                                        <strong>{{ notification.title }}</strong>
                                        <br>
                                        <small class="text-muted">{{ notification.content|truncatewords:10 }}</small>
                                    </td>
                                    <td>
                                        <span class="badge bg-info">{{ notification.get_target_roles_display }}</span>
                                    </td>
                                    <td>
                                        <span class="badge bg-{{ notification.get_priority_class }}">
                                            {{ notification.get_priority_display }}
                                        </span>
                                    </td>
                                    <td>
                                        {% if notification.is_current %}
                                        <span class="badge bg-success">有效</span>
                                        {% elif notification.end_date < now %}
                                        <span class="badge bg-secondary">过期</span>
                                        {% elif not notification.is_active %}
                                        <span class="badge bg-warning">停用</span>
                                        {% else %}
                                        <span class="badge bg-info">未开始</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <small>
                                            {{ notification.start_date|date:"Y-m-d H:i" }}<br>
                                            至 {{ notification.end_date|date:"Y-m-d H:i" }}
                                        </small>
                                    </td>
                                    <td>
                                        <small>{{ notification.created_by.username }}</small>
                                        <br>
                                        <small class="text-muted">{{ notification.created_at|date:"Y-m-d" }}</small>
                                    </td>
                                    <td>
                                        <div class="btn-group btn-group-sm">
                                            <a href="{% url 'notifications:notification_edit' notification.pk %}" 
                                               class="btn btn-outline-primary">
                                                <i class="bi bi-pencil"></i>
                                            </a>
                                            <form method="post" action="{% url 'notifications:notification_toggle' notification.pk %}" 
                                                  class="d-inline">
                                                {% csrf_token %}
                                                <button type="submit" class="btn btn-outline-{% if notification.is_active %}warning{% else %}success{% endif %}">
                                                    <i class="bi bi-{% if notification.is_active %}pause{% else %}play{% endif %}"></i>
                                                </button>
                                            </form>
                                            <a href="{% url 'notifications:notification_delete' notification.pk %}" 
                                               class="btn btn-outline-danger">
                                                <i class="bi bi-trash"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>

                    <!-- 分页 -->
                    {% if notifications.has_other_pages %}
                    <nav aria-label="Page navigation">
                        <ul class="pagination justify-content-center mt-4">
                            {% if notifications.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ notifications.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}">上一页</a>
                            </li>
                            {% endif %}

                            {% for i in notifications.paginator.page_range %}
                            <li class="page-item {% if notifications.number == i %}active{% endif %}">
                                <a class="page-link" href="?page={{ i }}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}">{{ i }}</a>
                            </li>
                            {% endfor %}

                            {% if notifications.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ notifications.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}">下一页</a>
                            </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}
                </div>
            </div>
            {% else %}
            <div class="text-center py-5">
                <div class="text-muted">
                    <i class="bi bi-bell" style="font-size: 3rem;"></i>
                    <h4 class="mt-3">暂无通知</h4>
                    <p>您还没有创建任何通知，点击"创建通知"开始发布系统通知。</p>
                    <a href="{% url 'notifications:notification_create' %}" class="btn btn-primary mt-2">
                        <i class="bi bi-plus-circle"></i> 创建通知
                    </a>
                </div>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
